<?php
global $_GPC, $_W;

$GLOBALS['frames'] = $this->getMainMenu();
$branch_list = pdo_getall('yzzc_sun_branch',['status'=>1]);
$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where uniacid =" . $_W['uniacid'];
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and name LIKE  '%$op%'";
    }
    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_shuttle') . "{$where} ORDER BY id DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    foreach ($info as $key => $value) {
        $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
        $user = pdo_get('yzzc_sun_user',['id'=>$value['uid']],['user_name','user_tel']);
        $info[$key]['user'] = $user;

        $sql = 'SELECT name FROM ' . tablename('yzzc_sun_branch') . " where id in (".$value['branch_ids'].")" ;
        $branch_info = pdo_fetchall($sql);
        $branch_info = array_column($branch_info,'name');
        $info[$key]['branch_info'] = $branch_info;
    }

    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_shuttle") . $where);
    $pager = pagination($total, $page, $size);

    include $this->template('web/shuttle/list');
    exit();
} elseif ($op == 'add') {
    if (checksubmit('submit')) {
        if (empty($_GPC['phone'])) {
            message('会员手机号不能为空', $this->createWebUrl('shuttle', array('op'=>'add')), 'error');
        }

        $user_info = pdo_get('yzzc_sun_user', ['user_tel' => $_GPC['phone'], 'uniacid' => $_W['uniacid']]);
        if (!$user_info) {
            message('会员尚未注册', $this->createWebUrl('shuttle', array('op'=>'add')), 'error');
        }

        $shuttle = pdo_get('yzzc_sun_shuttle',['uid'=>$user_info['id']]);
        if ($shuttle){
            message('该会员已经添加过了', $this->createWebUrl('shuttle', array('op'=>'add')), 'error');
        }

        $data = [
            'uid' => $user_info['id'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'branch_ids' => implode(',',$_GPC['branch_ids']),
            'createtime' => time()
        ];
        $result = pdo_insert('yzzc_sun_shuttle', $data);
        if ($result) {
            message('提交成功', $this->createWebUrl('shuttle', array()), 'success');
        } else {
            message('提交失败', $this->createWebUrl('shuttle', array('op'=>'add')), 'error');
        }
    }
    include $this->template('web/shuttle/add');
    return;
} elseif ($op == 'edit') {

    $info = pdo_get('yzzc_sun_shuttle', ['uniacid' => $_W['uniacid'], 'id' => $_GPC['id']]);

    $info['branch_ids'] = $info['branch_ids'] ? explode(',',$info['branch_ids']) : '';
    if (checksubmit('submit')) {
        $data = [
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'branch_ids' => implode(',',$_GPC['branch_ids']),
            'createtime' => time()
        ];
        $result = pdo_update('yzzc_sun_shuttle', $data, [
            'id' => $_GPC['id'],
            'uniacid' => $_W['uniacid']
        ]);
        if ($result) {
            message('修改成功', $this->createWebUrl('storelist', array('op' => 'display')), 'success');
        } else {
            message('修改失败', $this->createWebUrl('storelist', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
        }
    }
    include $this->template('web/shuttle/edit');
} elseif ($op == 'delete') {
    $res = pdo_delete('yzzc_sun_shuttle', array('id' => $_GPC['id']));
    if ($res) {
        message('操作成功', $this->createWebUrl('shuttle', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
} elseif ($op == 'change') {
    $where = ['store_id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_store', $where,['uid','uniacid','province','city','area','address','lng','lat','ranges','service_tel','mobile','pic','title']);
    if ($store_info['status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('storelist', array()), 'error');
    }

    $data = ['status' => $_GPC['status']];
    $platform_fee_ = $payout_fee = 0;
    if (isset($_GPC['remark']) && $_GPC['remark']) {
        $data['remark'] = $_GPC['remark'];
    }else{
        $platform_fee_ = $platform_fee['platform_fee'];
        if (isset($_GPC['platform_fee']) && $_GPC['platform_fee']) {
            $platform_fee_ = $_GPC['platform_fee'];
        }
        $payout_fee = $platform_fee['payout_fee'];
        if (isset($_GPC['payout_fee']) && $_GPC['payout_fee']) {
            $payout_fee = $_GPC['payout_fee'];
        }
    }
    $res = pdo_update('yzzc_sun_store', $data, $where);
    if ($res) {
        if ($data['status'] == 1){
            //审核通过 创建门店
            $uid = $store_info['uid'];
            $store_info['type'] = 2;
            $store_info['status'] = 1;
            $store_info['platform_fee'] = $platform_fee_;
            $store_info['payout_fee'] = $payout_fee;
            $store_info['shop_tel'] = $store_info['mobile'];
            $store_info['name'] = $store_info['title'];
            $store_info['store_id'] = $_GPC['id'];

            unset($store_info['mobile'],$store_info['uid'],$store_info['title']);
            pdo_insert('yzzc_sun_branch',$store_info);
            $branch_id = pdo_insertid();

            //创建超级管理员
            $branch_admin = [
                'bid' => $branch_id,
                'uid' => $uid,
                'auth'=> 1,
                'uniacid' => $store_info['uniacid'],
                'createtime' => time()
            ];
            pdo_insert('yzzc_sun_branch_admin',$branch_admin);
        }

        message('操作成功', $this->createWebUrl('storelist', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
}
dd(123);
